package com.nari.customer.service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.nari.baseinfo.dao.ICommunityDao;
import com.nari.basic.dao.IUserDao;
import com.nari.basic.model.IUser;
import com.nari.coreframe.service.GenericServiceImpl;
import com.nari.customer.bo.UserComplaint;
import com.nari.customer.dao.ICustomerComplaintsDao;
import com.nari.platform.model.PageCond;
import com.nari.platform.model.ResponseMsg;
import com.nari.platform.model.ResultWithPageCond;

public class CustomerComplaintsService extends
		GenericServiceImpl<UserComplaint, String> implements
		ICustomerComplaintService {
	private ICommunityDao iCommunityDao;
	private ICustomerComplaintsDao customerComplaintsDao;
	private IUserDao iUserDao;

	public ResultWithPageCond<UserComplaint> queryForPage(String buildingId,
			String projectId, String communityId, PageCond pageCond) {
		ICustomerComplaintsDao dao = (ICustomerComplaintsDao) this
				.getiGenericDao();
		ResultWithPageCond<UserComplaint> ls = dao.queryForPage(buildingId,
				projectId, communityId, pageCond);
		return ls;
	}

	public Map<String, String> queryReferCommunityMap(List<UserComplaint> list)
			throws Exception {
		List<String> communityIDList = null;
		if (list != null && list.size() > 0) {
			communityIDList = new ArrayList<String>();
			for (UserComplaint uc : list) {
				communityIDList.add(uc.getCommunityId());
			}
		}
		return iCommunityDao.queryIdNameMapByIDList(communityIDList);
	}

	public ResponseMsg saveOrUpdateUserComplaint(UserComplaint userComplaint)
			throws Exception {
		try {
			this.getiGenericDao().saveEntity(userComplaint);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return ResponseMsg.getInstance(true, "");
	}

	public ResponseMsg deleteObjectById(String complaintId) throws Exception {
		this.deleteEntityById(complaintId);
		return ResponseMsg.getInstance(true, "");
	}

	public UserComplaint queryForDetail(String complaintId) throws Exception {
		UserComplaint obj = this.queryEntityById(complaintId);
		IUser user = iUserDao.queryEntityById(obj.getUserId());
		obj.setLoginName(user.getUserID());
		if (obj.getReplyNo() != null && !"".equals(obj.getReplyNo())) {
			IUser user2 = iUserDao.queryEntityById(obj.getReplyNo());
			if (user2 != null) {
				obj.setReplyName(user2.getUserID());
			}
		}
		return obj;
	}

	public ResponseMsg updateObject(UserComplaint userComplaint) {
		ICustomerComplaintsDao dao = (ICustomerComplaintsDao) this
				.getiGenericDao();
		dao.updateObject(userComplaint);
		return ResponseMsg.getInstance(true, "");
	}

	public ICustomerComplaintsDao getCustomerComplaintsDao() {
		return customerComplaintsDao;
	}

	public void setCustomerComplaintsDao(
			ICustomerComplaintsDao customerComplaintsDao) {
		this.customerComplaintsDao = customerComplaintsDao;
	}

	public ICommunityDao getiCommunityDao() {
		return iCommunityDao;
	}

	public void setiCommunityDao(ICommunityDao iCommunityDao) {
		this.iCommunityDao = iCommunityDao;
	}

	public IUserDao getiUserDao() {
		return iUserDao;
	}

	public void setiUserDao(IUserDao iUserDao) {
		this.iUserDao = iUserDao;
	}

}
